package com.starrin.trashtalk;

import com.starrin.trashtalk.dao.PlayerDAO;
import com.starrin.trashtalk.dao.PlayerDAOImpl;
import com.starrin.trashtalk.model.Player;
import com.starrin.trashtalk.util.EmailSender;
import com.starrin.trashtalk.util.FacesMessageUtil;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import java.io.Serializable;
import java.util.*;

@ManagedBean
@ViewScoped
public class RegisterUserBean implements Serializable{
    private String firstName;
    private String lastName;
    private String month;
    private String day;
    private String year;
    private String email;
    private String emailVerify;
    private String password;
    private String sex;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    private PlayerDAO playerDAO = new PlayerDAOImpl();

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public String getDay() {
        return day;
    }

    public void setDay(String day) {
        this.day = day;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmailVerify() {
        return emailVerify;
    }

    public void setEmailVerify(String emailVerify) {
        this.emailVerify = emailVerify;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public List<SelectItem> getDaysOfMonth(){
        List<SelectItem> list = new ArrayList<>();
        for(int i=1; i<=31; i++) {
            list.add(new SelectItem(String.valueOf(i), String.valueOf(i)));
        }
        return list;
    }

    public List<SelectItem> getYears(){
        List<SelectItem> list = new ArrayList<>();
        for(int i =1914; i<=2014; i++) {
            list.add(new SelectItem(String.valueOf(i), String.valueOf(i)));
        }
        return list;
    }

    public String register() {
        //Call DAO..
        if(!email.equals(emailVerify)) {
            FacesMessageUtil.addError("You emails don't match");
            return null;
        }
        Player exitingPlayer = playerDAO.findPlayer(email);
        if(exitingPlayer != null) {
            FacesMessageUtil.addError("An account with the same email already exist.");
            return null;
        }

        Calendar dateOfBirth = new GregorianCalendar(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));
        playerDAO.registerNewPlayer(firstName, lastName, email, dateOfBirth.getTime(), sex, password);
        EmailSender emailSender = new EmailSender();

        String body = "<html>" +
                "<h1>Welcome to Trash Talk " + firstName +"</h1>" +
                "<div>Your login details:</div>" +
                "<div>Username: " + email + "</div>" +
                "<div>Password: " + password + "</div>" +
                "<div>Use this link to login: <a href=\"http://www.trashtalk.se/trashtalk\">http://www.trashtalk.se/<a/>";
        //emailSender.sendEmail(email, "Welcome to TrashTalk", body);
        FacesMessageUtil.addInfo("Registration successful! You can now login using your email and password:");
        return "success";
    }

    public void remind() {
        //Call DAO..
        EmailSender emailSender = new EmailSender();
        String body = "<html>" +
                "<div>Your login details:</div>" +
                "<div>Username: " + email + "</div>" +
                "<div>Password: " + password + "</div>" +
                "<div>Use this link to login: <a href=\"http://www.trashtalk.se/trashtalk\">http://www.trashtalk.se/<a/>";
        emailSender.sendEmail(email, "Trash Talk login details", body);
    }
}
